import { getCreatingHistory } from "@/api/aiDraw";
import { CardStatus } from "@/utils/enums";
import { useUserStore } from "@/store/index";

const userStore = useUserStore();

export default class UseCreatingHistory {
  public optionsList = [
    {
      label: "全部",
      value: "",
    },
    {
      label: "未发布",
      value: CardStatus.UNPUBLISHED,
    },
    {
      label: "已发布",
      value: CardStatus.PUBLISHED,
    },
    {
      label: "待审核",
      value: CardStatus.PENDING_APPROVAL,
    },
  ];
  public currentOptions = ref("");
  public loading = ref<boolean>(false);
  public noMore = ref<boolean>(false);
  public keyWord = ref(""); // 搜索条件
  pageOptions = ref({
    pageSize: 20,
    pageNum: 1,
  });
  public cardList = ref<any[]>([]); // 列表

  // 查询创作历史卡片
  queryCreatingHistory = async (reset = true) => {
    try {
      this.loading.value = true;
      this.noMore.value = false;
      const data = await getCreatingHistory(
        {
          cardStatus: this.currentOptions.value,
          userUuid: userStore.userInfo?.userUuid || "",
          vagueQuery: this.keyWord.value,
        },
        this.pageOptions.value,
      );
      if (reset) {
        this.cardList.value = data.rows || [];
      } else {
        this.cardList.value = [...this.cardList.value, ...data.rows];
      }
      if (!data.rows.length || data.rows.length < this.pageOptions.value.pageSize) {
        this.noMore.value = true;
      }
    } catch (err) {
      console.error(err);
      this.cardList.value = [];
    } finally {
      this.loading.value = false;
    }
  };
}
